package com.shushan.permission;

import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.android.permission.rom.RomType;
import com.android.permission.rom.RomUtils;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.shushanbao.R;
import com.shushan.ui.CustomDialog;
import com.shushan.util.OpenSettingUtils;

import static com.android.permission.rom.RomType.SMARTISAN_ROM;

/**
 * @ClassName: PowerManagerActivity
 * @Desciption: 关闭省电模式
 * @author: yichaohua
 * @date: 2018-05-28
 */
public class PowerManagerActivity extends NavigationLoseActivity implements View.OnClickListener {

    private static final String TAG = "OpenWindowActivity";

    private LinearLayout huaweiLayout1,huaweiLayout2,huaweiLayout3;
    private LinearLayout oppoLayout;
    private LinearLayout vivoLayout,vivoLayout1;
    private LinearLayout xiaomiLayout;
    private LinearLayout smartisanLayout;

    private Button nextButton;
    private Button okButton;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.power_manager_activity);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        initData();
    }

    private void initView() {
        huaweiLayout1 = findViewById(R.id.power_manager_huawei_layout1);
        huaweiLayout1.setOnClickListener(this);
        huaweiLayout2 = findViewById(R.id.power_manager_huawei_layout2);
        huaweiLayout2.setOnClickListener(this);
        huaweiLayout3 = findViewById(R.id.power_manager_huawei_layout3);
        huaweiLayout3.setOnClickListener(this);
        oppoLayout = findViewById(R.id.power_manager_oppo_layout);
        oppoLayout.setOnClickListener(this);
        vivoLayout = findViewById(R.id.power_manager_vivo_layout);
        vivoLayout.setOnClickListener(this);
        vivoLayout1 = findViewById(R.id.power_manager_vivo_layout1);
        xiaomiLayout = findViewById(R.id.power_manager_xiaomi_layout);
        xiaomiLayout.setOnClickListener(this);
        smartisanLayout = findViewById(R.id.power_manager_smartisan_layout);
        smartisanLayout.setOnClickListener(this);
        nextButton = findViewById(R.id.power_manager_next_btn);
        nextButton.setOnClickListener(this);
        okButton = findViewById(R.id.power_manager_ok_btn);
        okButton.setOnClickListener(this);
    }

    private void initData() {
        RomType romType = RomUtils.getRomType(this);
        Log.i(TAG,"romType=" + romType.toString());

        if(romType == SMARTISAN_ROM){
            setTitle("开启后台常驻");
        }else {
            setTitle("关闭省电管理");
        }


        switch (romType) {
            case MIUI_ROM:
                huaweiLayout1.setVisibility(View.GONE);
                huaweiLayout2.setVisibility(View.GONE);
                huaweiLayout3.setVisibility(View.GONE);
                oppoLayout.setVisibility(View.GONE);
                vivoLayout.setVisibility(View.GONE);
                xiaomiLayout.setVisibility(View.VISIBLE);
                smartisanLayout.setVisibility(View.GONE);
                break;
            case EMUI_ROM:
                oppoLayout.setVisibility(View.GONE);
                vivoLayout.setVisibility(View.GONE);
                xiaomiLayout.setVisibility(View.GONE);
                smartisanLayout.setVisibility(View.GONE);
                if(RomUtils.getEmuiLeval(this) >= 14){
                    huaweiLayout1.setVisibility(View.GONE);
                    huaweiLayout2.setVisibility(View.GONE);
                    huaweiLayout3.setVisibility(View.VISIBLE);
                }else if(RomUtils.getEmuiLeval(this) >= 11){
                    huaweiLayout1.setVisibility(View.GONE);
                    huaweiLayout2.setVisibility(View.VISIBLE);
                    huaweiLayout3.setVisibility(View.GONE);
                }else {
                    huaweiLayout1.setVisibility(View.VISIBLE);
                    huaweiLayout2.setVisibility(View.GONE);
                    huaweiLayout3.setVisibility(View.GONE);
                }
                break;
            case COLOROS_ROM:
                huaweiLayout1.setVisibility(View.GONE);
                huaweiLayout2.setVisibility(View.GONE);
                huaweiLayout3.setVisibility(View.GONE);
                oppoLayout.setVisibility(View.VISIBLE);
                vivoLayout.setVisibility(View.GONE);
                xiaomiLayout.setVisibility(View.GONE);
                smartisanLayout.setVisibility(View.GONE);
                break;
            case FUNTOUCHOS_ROM:
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                    vivoLayout1.setVisibility(View.VISIBLE);
                }else {
                    vivoLayout1.setVisibility(View.GONE);
                }
                huaweiLayout1.setVisibility(View.GONE);
                huaweiLayout2.setVisibility(View.GONE);
                huaweiLayout3.setVisibility(View.GONE);
                oppoLayout.setVisibility(View.GONE);
                vivoLayout.setVisibility(View.VISIBLE);
                xiaomiLayout.setVisibility(View.GONE);
                smartisanLayout.setVisibility(View.GONE);
                break;
            case SMARTISAN_ROM:
                huaweiLayout1.setVisibility(View.GONE);
                huaweiLayout2.setVisibility(View.GONE);
                huaweiLayout3.setVisibility(View.GONE);
                oppoLayout.setVisibility(View.GONE);
                vivoLayout.setVisibility(View.GONE);
                xiaomiLayout.setVisibility(View.GONE);
                smartisanLayout.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }

    }

    @Override
    protected void onLeftBackward() {
        showBackDialog();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.power_manager_next_btn:
                OpenSettingUtils.openPowerSetting(this);
                break;
            case R.id.power_manager_ok_btn:
                onBackAction(500);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                showBackDialog();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showBackDialog() {
        new CustomDialog.Builder(this)
                .setTitle("关闭省电模式")
                .setMessage("是否已关闭省电模式？")
                .setNegativeButton("已关闭", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        onBackAction(500);
                    }
                })
                .setPositiveButton("未关闭", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        OpenSettingUtils.openPowerSetting(PowerManagerActivity.this);
                    }
                })
                .create().show();
    }
}
